<?php
/*
You do not need to modify this script.
*/

// Bring in XML-RPC functionality and configuration
include('xmlrpc.php');
include('server_config.php');

// Fault code definitions
define("FAULTCODE_METHOD_NOT_SUPPORTED", 	-32601);
define("FAULTCODE_INVALID_INPUT",			-32000);
define("FAULTCODE_INVALID_KEY", 			-32001);
define("FAULTCODE_CUSTOM", 					-32099);

// Create the XML-RPC server object with the appropriate function mappings
$server = new IXR_Server(array(
    'pbbgwarp.doFanReward_v1' => 'doFanReward_v1',
    'pbbgwarp.doVoteReward_v1' => 'doVoteReward_v1',
    'pbbgwarp.getGameStats_v1' => 'getGameStats_v1',
    'pbbgwarp.getPlayerStats_v1' => 'getPlayerStats_v1',
    'pbbgwarp.isPlayerKeyValid_v1' => 'isPlayerKeyValid_v1'
));








function doFanReward_v1($inParams) {

	// Is this method implemented?
	if (!IMPLEMENTED_DOFANREWARD_V1) {
		return new IXR_Error(FAULTCODE_METHOD_NOT_SUPPORTED,'This method has not been implemented.');
	}
	
	// Validate the input
	if (
		!isset($inParams['key']) || !is_string($inParams['key'])
		|| !isset($inParams['screenname']) || !is_string($inParams['screenname'])
		|| !isset($inParams['verified']) || !is_bool($inParams['verified'])
		) {
		return new IXR_Error(FAULTCODE_INVALID_INPUT, 'Invalid or missing input parameters.');
	}
	
	// Verify the integration key
	if ($inParams['key'] != INTEGRATION_KEY) {
		return new IXR_Error(FAULTCODE_INVALID_KEY, 'Invalid integration key.');
	}
	
	// Connect to db
	config_dbconnect();
	
	// Return the results
	return config_doFanReward_v1($inParams['screenname'], $inParams['verified']);


} // doFanReward_v1 function








function doVoteReward_v1($inParams) {

	// Is this method implemented?
	if (!IMPLEMENTED_DOVOTEREWARD_V1) {
		return new IXR_Error(FAULTCODE_METHOD_NOT_SUPPORTED,'This method has not been implemented.');
	}
	
	// Validate the input
	if (
		!isset($inParams['key']) || !is_string($inParams['key'])
		|| !isset($inParams['satdomain']) || !is_string($inParams['satdomain'])
		|| !isset($inParams['userid']) || !is_string($inParams['userid'])
		|| !isset($inParams['votecounted']) || !is_bool($inParams['votecounted'])
		) {
		return new IXR_Error(FAULTCODE_INVALID_INPUT, 'Invalid or missing input parameters.');
	}
	
	// Verify the integration key
	if ($inParams['key'] != INTEGRATION_KEY) {
		return new IXR_Error(FAULTCODE_INVALID_KEY, 'Invalid integration key.');
	}
	
	// Connect to db
	config_dbconnect();
	
	// Return the results
	return config_doVoteReward_v1($inParams['satdomain'], $inParams['userid'], $inParams['votecounted']);


} // doVoteReward_v1 function

function getGameStats_v1($inParams) {
	// Is this method implemented?
	if (!IMPLEMENTED_GETGAMESTATS_V1) {
		return new IXR_Error(FAULTCODE_METHOD_NOT_SUPPORTED,'This method has not been implemented.');
	}
	
	// Validate the input
	if (!isset($inParams['key']) || !is_string($inParams['key'])) {
		return new IXR_Error(FAULTCODE_INVALID_INPUT, 'Invalid or missing input parameters.');
	}
	
	// Verify the integration key
	if ($inParams['key'] != INTEGRATION_KEY) {
		return new IXR_Error(FAULTCODE_INVALID_KEY, 'Invalid integration key.');
	}
	
	// Connect to db
	config_dbconnect();
	
	// Start building the output
	$outArray = array();
	
	// Get total, effective, online players
	if ($numPlayers = config_getGameStats_v1_totalPlayers())
		$outArray['totalplayers'] = $numPlayers;
	if ($numPlayers = config_getGameStats_v1_effectivePlayers())
		$outArray['effectiveplayers'] = $numPlayers;
	if ($numPlayers = config_getGameStats_v1_onlinePlayers())
		$outArray['onlineplayers'] = $numPlayers;
	
	
	// Get custom stats
	if (!is_null(GETGAMESTATS_V1_CUSTOMSTAT1NAME)) {
		$outArray['customstat1name'] = GETGAMESTATS_V1_CUSTOMSTAT1NAME;
		$outArray['customstat1value'] = config_getGameStats_v1_customStat1();
	}
	if (!is_null(GETGAMESTATS_V1_CUSTOMSTAT2NAME)) {
		$outArray['customstat2name'] = GETGAMESTATS_V1_CUSTOMSTAT2NAME;
		$outArray['customstat2value'] = config_getGameStats_v1_customStat2();
	}
	if (!is_null(GETGAMESTATS_V1_CUSTOMSTAT3NAME)) {
		$outArray['customstat3name'] = GETGAMESTATS_V1_CUSTOMSTAT3NAME;
		$outArray['customstat3value'] = config_getGameStats_v1_customStat3();
	}
	if (!is_null(GETGAMESTATS_V1_CUSTOMSTAT4NAME)) {
		$outArray['customstat4name'] = GETGAMESTATS_V1_CUSTOMSTAT4NAME;
		$outArray['customstat4value'] = config_getGameStats_v1_customStat4();
	}
	if (!is_null(GETGAMESTATS_V1_CUSTOMSTAT5NAME)) {
		$outArray['customstat5name'] = GETGAMESTATS_V1_CUSTOMSTAT5NAME;
		$outArray['customstat5value'] = config_getGameStats_v1_customStat5();
	}
	
	// Return the results
	return $outArray;


} // getGameStats_v1 function

function getPlayerStats_v1($inParams) {
	// Is this method implemented?
	if (!IMPLEMENTED_GETPLAYERSTATS_V1) {
		return new IXR_Error(FAULTCODE_METHOD_NOT_SUPPORTED,'This method has not been implemented.');
	}
	
	// Validate the input
	if (
		!isset($inParams['key']) || !is_string($inParams['key']) || !isset($inParams['screennames']) || !is_array($inParams['screennames'])
		) {
		return new IXR_Error(FAULTCODE_INVALID_INPUT, 'Invalid or missing input parameters.');
	}
	foreach ($inParams['screennames'] as $curScreenName) {
		if (!is_string($curScreenName)) {
			return new IXR_Error(FAULTCODE_INVALID_INPUT, 'Invalid or missing input parameters.');
		}
	}
	
	// Verify the integration key
	if ($inParams['key'] != INTEGRATION_KEY) {
		return new IXR_Error(FAULTCODE_INVALID_KEY, 'Invalid integration key.');
	}
	
	// Connect to db
	config_dbconnect();
	
	// Add stat1(name|data|sort) to the response
	$outArray = array();
	$outArray['stat1name'] = GETPLAYERSTATS_V1_STAT1NAME;
	$outArray['stat1sort'] = GETPLAYERSTATS_V1_STAT1SORT;

	// Add stat2(name|data|sort) to the response if present
	if (!is_null(GETPLAYERSTATS_V1_STAT2NAME) && GETPLAYERSTATS_V1_STAT2NAME != '') {
		$outArray['stat2name'] = GETPLAYERSTATS_V1_STAT2NAME;
		$outArray['stat2sort'] = GETPLAYERSTATS_V1_STAT2SORT;
	}
	
	// Add stat3(name|data|sort) to the response if present
	if (!is_null(GETPLAYERSTATS_V1_STAT3NAME) && GETPLAYERSTATS_V1_STAT3NAME != '') {
		$outArray['stat3name'] = GETPLAYERSTATS_V1_STAT3NAME;
		$outArray['stat3sort'] = GETPLAYERSTATS_V1_STAT3SORT;
	}
	
	// Build the playerstats array
	$outPlayerStats = array();
	foreach ($inParams['screennames'] as $curScreenName) {
		if ($playerArr = config_getPlayerStats_v1($curScreenName)) {
			$playerArr['screenname'] = $curScreenName;
			array_push($outPlayerStats, $playerArr);
		}
	}
	$outArray['playerstats'] = $outPlayerStats;
	
	// Return the results
	return $outArray;


} // getPlayerStats_v1 function

function isPlayerKeyValid_v1($inParams) {
	// Is this method implemented?
	if (!IMPLEMENTED_ISPLAYERKEYVALID_V1) {
		return new IXR_Error(FAULTCODE_METHOD_NOT_SUPPORTED,'This method has not been implemented.');
	}
	
	// Validate the input
	if (
		!isset($inParams['key']) || !is_string($inParams['key'])
		|| !isset($inParams['screenname']) || !is_string($inParams['screenname'])
		|| !isset($inParams['playerkey']) || !is_string($inParams['playerkey'])
		) {
		return new IXR_Error(FAULTCODE_INVALID_INPUT, 'Invalid or missing input parameters.');
	}
	
	// Verify the integration key
	if ($inParams['key'] != INTEGRATION_KEY) {
		return new IXR_Error(FAULTCODE_INVALID_KEY, 'Invalid integration key.');
	}

	// Connect to db
	config_dbconnect();
	
	// Return the results
	return array (
		'isvalid' => config_isPlayerKeyValid_v1($inParams['screenname'], $inParams['playerkey'])
	);
} // isPlayerKeyValid_v1 function
?>
